package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Scanner;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    /**插入新套餐*/
    @Override
    @Transactional
    public void insertSetmeal(SetmealDTO setmealDTO) {

        //判空数据
        if(setmealDTO == null){throw new DeletionNotAllowedException(MessageConstant.DATA_IS_NULL);}


        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.insertSetmeal(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        setmealDishes.forEach(s->{
            s.setSetmealId(setmeal.getId());
        });

        setmealDishMapper.insertSetmealDish(setmealDishes);
    }

    /**套餐分页查询*/
    @Override
    public PageResult selectSetmealPage(SetmealPageQueryDTO setmealPageQueryDTO) {

        //判空数据
        if(setmealPageQueryDTO == null){throw new DeletionNotAllowedException(MessageConstant.DATA_IS_NULL);}


        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> setmealVOPage = setmealMapper.selectSetmealPage(setmealPageQueryDTO.getName(),setmealPageQueryDTO.getCategoryId(),setmealPageQueryDTO.getStatus());

        if(setmealVOPage.size() == 0){
            throw new DeletionNotAllowedException(MessageConstant.SELECT_NOTHING);
        }

        PageResult pageResult = new PageResult();

        pageResult.setTotal(setmealVOPage.getTotal());

        pageResult.setRecords(setmealVOPage.getResult());

        return pageResult;
    }
    /**套餐状态修改*/
    @Override
    @Transactional
    public void updateSetmealStatus(Integer status, Long id) {
        //判空数据
        if(status == null || id == null){throw new DeletionNotAllowedException(MessageConstant.DATA_IS_NULL);}

        //前端数据
        if(status == StatusConstant.ENABLE){
            //查询套餐包含了几种菜品
            List listids = setmealDishMapper.selectDishIdById(id);
            //判断要启售的套餐中是否存在未启售的菜品
            List list1 = setmealMapper.selectStatusByIds(listids);
            if(list1.contains(0)){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        setmealMapper.updateSetmealStatus(status,id);
    }

    /**根据id查询套餐*/
    @Override
    public Setmeal selectSetmealById(Long id) {
        Setmeal setmeal = setmealMapper.selectSetmealById(id);
        return setmeal;
    }

    /**套餐的修改*/
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //判断套餐数据
        if(setmealDTO == null){ throw new DeletionNotAllowedException(MessageConstant.SETMEAL_IS_NULL);}

        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealDTO,setmeal);
        //更新套餐
        setmealMapper.updateSetmeal(setmeal);

        //获取套餐和菜品关系表数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

//防止setmealDishes存在一条空数据
        if(setmealDTO == null && setmealDishes == null){ throw new DeletionNotAllowedException(MessageConstant.SETMEAL_IS_NULL);}

        //判断是否需要更改setmealDish表的数据
        if(setmealDishes != null && setmealDishes.size() != 0){
            //删除原有数据
            setmealDishMapper.deleteSetmealDishe(setmeal.getId());

            //将套餐更改的id传给setmealDishes
            setmealDishes.forEach(s->{
                s.setSetmealId(setmeal.getId());
            });

            //插入更改后的数据
            setmealDishMapper.insertSetmealDish(setmealDishes);
        }
    }

    /**根据Ids删除套餐*/
    @Override
    @Transactional
    public void deleteSetmealByIds(List<Long> ids) {
        //判断ids
        if(ids == null){ throw new DeletionNotAllowedException(MessageConstant.ID_IS_NULL);}

        //查询套餐启售数据
        List<Setmeal> list;
        list = setmealMapper.selectSetmealByIds(ids);
        //判断套餐是否启售状态
        if(list != null && list.size() != 0 && list.contains(0)) {

            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //根据ids删除套餐
        setmealMapper.deleteSetmealByIds(ids);

        //删除套餐的关联表数据
        setmealDishMapper.deleteDishSetmealByIds(ids);
        
    }
}
